package com.yuju.controller.hotel;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yuju.common.PageModel;
import com.yuju.common.Result;
import com.yuju.common.fileld.validator.FieldValidator;
import com.yuju.controller.base.BaseController;
import com.yuju.enums.ReturnCodeEnum;
import com.yuju.enums.ReturnStatusEnum;
import com.yuju.hotel.enums.CheckinStandardReturnCodeEnum;
import com.yuju.hotel.model.CheckinStandardModel;
import com.yuju.hotel.model.PriceDetailModel;
import com.yuju.hotel.service.ICheckinStandardService;
import com.yuju.util.Utils;
import com.yuju.vo.hotel.CheckinStandardVO;
/**
 * 
 * 类名：CheckinStandardController  <br />
 *
 * 功能：入住标准相关接口
 *
 * @author : zhousc <br />
 * @Date : 2017年11月29日 下午3:43:29  <br />
 * @version : 1.1 <br />
 */
@Controller
@RequestMapping("checkinStandard")
public class CheckinStandardController extends BaseController<CheckinStandardVO, CheckinStandardModel> {

	@Autowired
	private ICheckinStandardService checkinStandardService;
	/**
	 * 
	 * 功能：入住标准列表<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:27 <br/>
	 */
	@RequestMapping("list")
	@ResponseBody
	public Result list(@RequestBody JSONObject obj) {
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		// 参数校验
		Map<String, String> fail = FieldValidator.validateAll(checkinStandardVO, "pager", true);
		if (!fail.isEmpty()) {
			return validateError(fail);
		}
		Result result = new Result();
		PageModel pageModel = checkinStandardService.pageList(convert(checkinStandardVO), checkinStandardVO.getPage(), checkinStandardVO.getPageSize());
		if (Utils.isNotNull(pageModel) && Utils.isNotNull(pageModel.getRows())) {		
			result.setPageModel(pageModel);
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getDesc());
		} else {
			result = new Result();
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：入住标准保存<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:41 <br/>
	 */
	@RequestMapping("save")
	@ResponseBody
	public Result save(@RequestBody JSONObject obj) {
		Result result = new Result();
		int rows = 0;
		//映射前端传来的数据
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		
		if(Utils.isNotNull(obj.getString("btime"))){
			checkinStandardVO.setLimitCheckinBegin(Time.valueOf(obj.getString("btime")));
		}
		if(Utils.isNotNull(obj.getString("etime"))){
			checkinStandardVO.setLimitCheckinEnd(Time.valueOf(obj.getString("etime")));
		}
		if(Utils.isNotNull(obj.getString("ctime"))){
			checkinStandardVO.setCheckoutTime(Time.valueOf(obj.getString("ctime")));
		}
		if(Utils.isNotNull(obj.getString("otime"))){
			checkinStandardVO.setCheckoutOverTime(Time.valueOf(obj.getString("otime")));
		}
		if(Utils.isNotNull(obj.getString("rtime"))){
			checkinStandardVO.setRetentionTime(Time.valueOf(obj.getString("rtime")));
		}
		
		//全天房入住标准不添加价格明细
		if(checkinStandardVO.getCheckinType() != 2){
			JSONArray jsonArray = obj.getJSONArray("priceDetailModels");
			//将array数组转换成字符串  
			String js = jsonArray.toJSONString();
			//把字符串转换成集合  
			List<PriceDetailModel> priceDetailModels = (List<PriceDetailModel>) JSONObject.parseObject(js,  new TypeReference<ArrayList<PriceDetailModel>>() {});
			rows = checkinStandardService.saveCheckinStandard(convert(checkinStandardVO) ,priceDetailModels);
		} else {
			rows = checkinStandardService.saveCheckinStandard(convert(checkinStandardVO) ,null);
		}
		
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_FAILURE.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_SAVE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：入住标准更新<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午2:24:55 <br/>
	 */
	@RequestMapping("update")
	@ResponseBody
	public Result update(@RequestBody JSONObject obj) {
		Result result = new Result();
		
		//映射前端数据
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		
		if(Utils.isNotNull(obj.getString("btime"))){
			checkinStandardVO.setLimitCheckinBegin(Time.valueOf(obj.getString("btime")));
		}
		if(Utils.isNotNull(obj.getString("etime"))){
			checkinStandardVO.setLimitCheckinEnd(Time.valueOf(obj.getString("etime")));
		}
		if(Utils.isNotNull(obj.getString("ctime"))){
			checkinStandardVO.setCheckoutTime(Time.valueOf(obj.getString("ctime")));
		}
		if(Utils.isNotNull(obj.getString("otime"))){
			checkinStandardVO.setCheckoutOverTime(Time.valueOf(obj.getString("otime")));
		}
		if(Utils.isNotNull(obj.getString("rtime"))){
			checkinStandardVO.setRetentionTime(Time.valueOf(obj.getString("rtime")));
		}
		int rows = 0;
		//全天房入住标准不更新价格明细
		if(checkinStandardVO.getCheckinType() != 2){
			JSONArray jsonArray = obj.getJSONArray("priceDetailModels");
			//将array数组转换成字符串  
			String js = jsonArray.toJSONString();
			//把字符串转换成集合  
			List<PriceDetailModel> priceDetailModels = (List<PriceDetailModel>) JSONObject.parseObject(js,  new TypeReference<ArrayList<PriceDetailModel>>() {});
			rows = checkinStandardService.updateCheckinStandard(convert(checkinStandardVO) ,priceDetailModels);
		} else {
			rows = checkinStandardService.updateCheckinStandard(convert(checkinStandardVO) ,null);
		}
				
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_UPDATE_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_UPDATE_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_UPDATE_FAILURE.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_UPDATE_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：删除入住标准<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午3:47:58 <br/>
	 */
	@RequestMapping("delete")
	@ResponseBody
	public Result delete(@RequestBody JSONObject obj) {
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		Result result = new Result();
		int rows = checkinStandardService.del(checkinStandardVO.getId());
		if (rows > 0) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_DEL_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_DEL_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_DEL_FAILURE.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_DEL_FAILURE.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：根据ID查询入住标准信息<br/>
	 *
	 * @author zhousc
	 * @Date 2017年11月29日 下午4:09:11 <br/>
	 */
	@RequestMapping("get")
	@ResponseBody
	public Result get(@RequestBody JSONObject obj) {
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		Result result = new Result();
		CheckinStandardModel model = checkinStandardService.findById(checkinStandardVO.getId());
		if (Utils.isNotNull(model)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(model);
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	
	/**
	 * 
	 * 功能：入住标准下拉<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月13日 下午3:12:00 <br/>
	 */
	@RequestMapping("getCheckinStandardDropdown")
	@ResponseBody
	public Result getCheckinStandardDropdown(@RequestBody JSONObject obj) {
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		Result result = new Result();
		List<CheckinStandardModel> list = checkinStandardService.list(convert(checkinStandardVO));
		if (Utils.isNotNull(list)) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setData(list);
			result.setReturnCode(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getCode());
			result.setReturnMsg(CheckinStandardReturnCodeEnum.CHECKINSTANDARD_QUERY_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.NO_DATA.getCode());
			result.setReturnMsg(ReturnCodeEnum.NO_DATA.getDesc());
		}
		return result;
	}
	/**
	 * 
	 * 功能：重名验证<br/>
	 *
	 * @author zhousc
	 * @Date 2017年12月14日 上午11:32:54 <br/>
	 */
	@RequestMapping("selectbyname")
	@ResponseBody
	public Result selectbyname(@RequestBody JSONObject obj) {
		CheckinStandardVO checkinStandardVO = obj.toJavaObject(CheckinStandardVO.class);
		Result result = new Result();
		boolean selectByName  = checkinStandardService.selectByName(convert(checkinStandardVO));
		if (selectByName) {
			result.setStatus(ReturnStatusEnum.SUCCESS.getCode());
			result.setMsg(ReturnStatusEnum.SUCCESS.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_SUCCESS.getDesc());
		} else {
			result.setStatus(ReturnStatusEnum.FAILURE.getCode());
			result.setMsg(ReturnStatusEnum.FAILURE.getDesc());
			result.setReturnCode(ReturnCodeEnum.VALID_REPEAT_FAILURE.getCode());
			result.setReturnMsg(ReturnCodeEnum.VALID_REPEAT_FAILURE.getDesc());
		}
		return result;
	}
}